#include "mainwindow.h"

#include <QLabel>
#include <QPushButton>
#include <QThreadPool>
#include "shelf.h"
#include "writer.h"
#include "reader.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
    QGridLayout *gridLayout = new QGridLayout(this);

    initDefaultParam();

    //-----
    gridLayout->addWidget(new QLabel("ENTER INPUT PARAM:"), 0, 0);

    //----- Shelf`s size
    gridLayout->addWidget(new QLabel("shelf`s size:"), 1, 0);
    shelfSizeParam = new QLineEdit(QString("%1").arg(shelfSize));
    addLineEdit(shelfSizeParam, gridLayout, 1, 1);

    //----- Reader`s count
    gridLayout->addWidget(new QLabel("reader`s count:"), 2, 0);
    rCountParam = new QLineEdit(QString("%1").arg(readerCount));
    addLineEdit(rCountParam, gridLayout, 2, 1);

    //----- Read`s time
    gridLayout->addWidget(new QLabel("read`s time (sec):"), 2, 2);
    rTimeParam = new QLineEdit(QString("%1").arg(readTime));
    addLineEdit(rTimeParam, gridLayout, 2, 3);

    //----- Writer`s count
    gridLayout->addWidget(new QLabel("writer`s count:"), 3, 0);
    wCountParam = new QLineEdit(QString("%1").arg(writerCount));
    addLineEdit(wCountParam, gridLayout, 3, 1);

    //----- Write`s time
    gridLayout->addWidget(new QLabel("write`s time (sec):"), 3, 2);
    wTimeParam = new QLineEdit(QString("%1").arg(writeTime));
    addLineEdit(wTimeParam, gridLayout, 3, 3);

    //-----
    QPushButton *okButton = new QPushButton("Ok");
    gridLayout->addWidget(okButton, 4, 3);
    connect(okButton, SIGNAL(clicked()), this, SLOT(okBtClicked()));

    //-----
    shelfProgress = new QProgressBar();
    initProgressBar(shelfProgress, gridLayout, "Shelf:", 5);

    activeProgress = new QProgressBar();
    initProgressBar(activeProgress, gridLayout, "Active:", 6);

    waitProgress = new QProgressBar();
    initProgressBar(waitProgress, gridLayout, "Wait:", 7);

    //-----
    QWidget *widget = new QWidget(this);
    widget->setLayout(gridLayout);
    setCentralWidget(widget);
}

void MainWindow::initDefaultParam()
{
    shelfSize = 20;
    readerCount = 10;
    readTime = 2;
    writerCount = 10;
    writeTime = 2;
}

void MainWindow::updateParam()
{
    shelfSize = shelfSizeParam->text().toInt();
    readerCount = rCountParam->text().toInt();
    readTime = rTimeParam->text().toInt();
    writerCount = wCountParam->text().toInt();
    writeTime = wTimeParam->text().toInt();

    shelfProgress->setMaximum(shelfSize);
    activeProgress->setMaximum(writerCount + readerCount);
    waitProgress->setMaximum(writerCount + readerCount);
}

void MainWindow::addLineEdit(QLineEdit *lineEdit, QGridLayout * gridLayout, int row, int col)
{
    lineEdit->setInputMask("999");
    gridLayout->addWidget(lineEdit, row, col);
}

void MainWindow::initProgressBar(QProgressBar *progress, QGridLayout * gridLayout, const QString &label, int row)
{
    progress->setMinimum(0);
    gridLayout->addWidget(new QLabel(label), row, 0);
    gridLayout->addWidget(progress, row, 1, 1, 4);
    progress->setTextVisible(true);
}

void MainWindow::shelfProgressSlot(){
    shelfProgress->setValue(shelfProgress->value() + 1);
}

void MainWindow::activeProgressSlot(){
    activeProgress->setValue(activeProgress->value() + 1);
    waitProgress->setValue(waitProgress->value() - 1);
}

void MainWindow::waitProgressSlot(){
    waitProgress->setValue(waitProgress->value() + 1);
    activeProgress->setValue(activeProgress->value() - 1);
}

void MainWindow::okBtClicked(){
    updateParam();

    Shelf *shelf = new Shelf(shelfSize);
    connect(shelf, SIGNAL(newBook()), this, SLOT(shelfProgressSlot()));

    QThreadPool *threadPool = QThreadPool::globalInstance();
    threadPool->setMaxThreadCount(readerCount + writerCount);

    for(int i = 0; i < writerCount; ++i){
        Writer *writer = new Writer(shelf, writeTime);
        connect(writer, SIGNAL(write()), this, SLOT(activeProgressSlot()));
        connect(writer, SIGNAL(edit()), this, SLOT(activeProgressSlot()));
        connect(writer, SIGNAL(wait()), this, SLOT(waitProgressSlot()));
        threadPool->start(writer);
    }

    for(int i = 0; i < readerCount; ++i){
        Reader *reader = new Reader(shelf, readTime);
        connect(reader, SIGNAL(read()), this, SLOT(activeProgressSlot()));
        connect(reader, SIGNAL(wait()), this, SLOT(waitProgressSlot()));
        threadPool->start(reader);
    }
}

MainWindow::~MainWindow()
{
}
